// loading in .bmp file information: http://rastertek.com/tertut02.html
// and http://braynzarsoft.net/index.php?p=D3D11HM
// date referenced: 8th February 2013

// slope texturing by using vertex normals: http://rastertek.com/tertut14.html
// date referenced 16th February 2013

// terrain blending by using alpha maps: http://rastertek.com/tertut09.html
// date referenced 20th February 2013

// skydome rendering http://rastertek.com/tertut10.html
// date referenced 20th February 2013

#ifndef DX11HEIGHTMAPTERRAIN_H
#define DX11HEIGHTMAPTERRAIN_H

#include "ConstantBuffer.h"
#include "DX11VertexData.h"
#include "DX11Water.h"
#include "Skydome.h"

#include <vector>
#include <D3D11.h>
#include <D3DX10math.h>

class Skydome;

struct HMTChunk
{
	HMTChunk()
	{
		pVertexBuffer = NULL;
		pConstantBuffer = NULL;
		pIndexBuffer = NULL;
		pTextureSamplerState = NULL;
		numOfIndices = 0;
	}
	~HMTChunk()
	{
		if (pVertexBuffer) pVertexBuffer->Release();
		if (pIndexBuffer) pIndexBuffer->Release();
		if (pConstantBuffer) pConstantBuffer->Release();
		if (pTextureSamplerState) pTextureSamplerState->Release();
	}
	void ClearVectorData()
	{
		vertexData.clear();
		indices.clear();
	}

	int numOfIndices;
	std::vector<VertexData> vertexData; // need to add uv + normal data
	std::vector<DWORD> indices;
	ID3D11Buffer* pVertexBuffer;
	ID3D11Buffer* pConstantBuffer;
	ID3D11SamplerState* pTextureSamplerState;
	ID3D11Buffer* pIndexBuffer;
	D3D11_BUFFER_DESC bd;
	D3D11_SUBRESOURCE_DATA initData;
	//ConstantBuffer cb;
};

class DX11HeightMapTerrain
{
public:
	DX11HeightMapTerrain();
	~DX11HeightMapTerrain();
	void Draw();
	void Update();

	bool Load(const std::string& filename);
	bool GenerateRandom(int width, int height);

private:
	void DrawTerrain();

private:
	int m_width;
	int m_height;

	//int m_numOfIndices;
	//D3DXVECTOR3* m_heightMap;
	//std::vector<D3DXVECTOR3> m_vertices; // need to add uv + normal data
	//std::vector<WORD> m_indices;
	//ID3D11Buffer* m_vertexBuffer;
	//ID3D11Buffer* m_pConstantBuffer;
	//ID3D11SamplerState* m_textureSamplerState;
	//ID3D11Buffer* m_indexBuffer;
	//D3D11_BUFFER_DESC m_bd;
	//D3D11_SUBRESOURCE_DATA m_initData;

	//ConstantBuffer m_cb;
	ConstantBuffer m_cb;
	std::vector<HMTChunk*> m_chunks;
	D3DXMATRIX m_world;

	DX11Water m_water;
	
	// water refraction vars
	ID3D11Texture2D* m_pWaterRenderTargetTexture;
	ID3D11RenderTargetView* m_pWaterRenderTargetView;
	ID3D11ShaderResourceView* m_pWaterShaderResourceView;

	ID3D11Texture2D* m_pWaterReflectionTargetTexture;
	ID3D11RenderTargetView* m_pWaterReflectionTargetView;
	ID3D11ShaderResourceView* m_pWaterReflectionSRV;

	Skydome m_dome;
	D3DXVECTOR4 m_waterTint;
};

#endif //DX11HEIGHTMAPTERRAIN_H
